#!/usr/bin/env python
# coding: utf-8

from playwright.sync_api import sync_playwright
from lxml import html

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto('https://2.taobao.com')
    page.wait_for_timeout(2000)
    page.get_by_text('最新发布').click()
    page.wait_for_timeout(2000)
    items = page.locator('//*[@id="root"]/div/div/div/div[contains(@class, "flex-col")]/div[contains(@class, "flex-wrap")]/div').all()
    for item in items:
        html_content = item.inner_html()
        html_obj = html.fromstring(html_content)

        head_img_element = html_obj.xpath('div/div/img')[0]
        head_img = head_img_element.get('src')

        title_element = html_obj.xpath('div/div/div/p')[0]
        title = title_element.text

        price_element = html_obj.xpath('div/div/div[contains(@class, "leading-none")]/div[contains(@class, "relative") and contains(@class, "xianyubeta")]')[0]
        price = price_element.text

        user_element = html_obj.xpath('div/div/div[contains(@class, "items-center")]/div/div')[0]
        username = user_element.text

        user_head_element = html_obj.xpath('div/div/div[contains(@class, "items-center")]/div/img')[0]
        user_head = user_head_element.get('src')

        print("="*42)
        print("商品图: ", head_img)
        print("商品标题: ", title)
        print("商品价格: ", price)
        print("卖家姓名: ", username)
        print("卖家头像: ", user_head)
    browser.close()
